<?php
// $Id: password_reset.admin.inc,v 1.1.2.6 2009/02/28 07:34:26 karthik Exp $

/**
 * Menu callback: List the security questions.
 */
function password_reset_admin_questions() {
  $result = db_query('SELECT pr.qid, pr.question, COUNT(pru.qid) as total FROM {password_reset} pr LEFT JOIN {password_reset_users} pru ON (pr.qid = pru.qid) GROUP BY pr.qid ORDER BY question');

  $rows = array();
  while ($question = db_fetch_object($result)) {
    $rows[] = array($question->question, $question->total, l(t('Edit'), 'admin/user/password_reset/edit/'. $question->qid), l(t('Delete'), 'admin/user/password_reset/delete/'. $question->qid));
  }

  if (empty($rows)) {
    $rows[] = array(array('data' => t('No questions found.'), 'colspan' => 4));
  }

  $header = array(t('Question'), t('Use count'), array('data' => t('Operations'), 'colspan' => 2));

  return theme('table', $header, $rows);
}

/**
 * Menu callback: Question edit page.
 */
function password_reset_admin_edit($form_state, $qid = NULL) {
  $form['question'] = array(
    '#type' => 'textfield',
    '#title' => t('Security question'),
    '#maxlength' => 255,
    '#description' => t("Example: 'What is your library card number?' or 'What is your mother's maiden name?'."),
    '#required' => TRUE
  );

  $form['submit'] = array('#type' => 'submit', '#value' => t('Add question'));

  if (arg(3) == 'edit' && isset($qid)) {
    $question = db_fetch_array(db_query("SELECT * FROM {password_reset} WHERE qid = %d", $qid));
    $form['question']['#default_value'] = $question['question'];
    $form['qid'] = array('#type' => 'value', '#value' => $question['qid']);
    $form['submit']['#value'] = t('Update question');
  }

  return $form;
}

/**
 * Process the password_reset question edit page form submission.
 */
function password_reset_admin_edit_submit($form, &$form_state) {
  if (isset($form_state['values']['qid'])) {
    password_reset_question_add($form_state['values'], 'qid');
  }
  else {
    password_reset_question_add($form_state['values']);
  }

  $form_state['redirect'] = 'admin/user/password_reset';
}

/**
 * Menu callback: Question delete page.
 */
function password_reset_admin_delete($form_state, $qid) {
  if ($info = db_fetch_object(db_query("SELECT * FROM {password_reset} WHERE qid = %d", $qid))) {
    $form['question'] = array('#type' => 'value', '#value' => $info->question);
    $form['qid'] = array('#type' => 'value', '#value' => $info->qid);

    return confirm_form(
      $form, 
      t('Are you sure you want to delete %question?', array('%question' => $info->question)),
      'admin/user/password_reset',
      t('Users who have chosen this as their security question will have to update their profiles. This action cannot be undone.'),
      t('Delete'),
      t('Cancel')
    );
  }
}

/**
 * Process question delete form submission.
 */
function password_reset_admin_delete_submit($form, &$form_state) {
  password_reset_question_delete($form_state['values']);
  $form_state['redirect'] = 'admin/user/password_reset';
}
